package com.syh.jn.scm.infrastructure.repository.impl.supplier.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.supplier.entity.base.SupplierInvoiceInfo;
import com.syh.jn.scm.domain.supplier.repository.base.SupplierSupplierInvoiceInfoRepository;
import com.syh.jn.scm.dto.supplier.base.invoiceInfo.InvoiceInfoVo;
import com.syh.jn.scm.infrastructure.repository.model.base.BusinessEntityInvoiceInfoModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

@Repository
public class SupplierInvoiceInfoRepositoryImpl extends AbstractAggregateRepository<SupplierInvoiceInfo, BusinessEntityInvoiceInfoModel, Long> implements SupplierSupplierInvoiceInfoRepository {

  @Override
  public Long add(SupplierInvoiceInfo supplierInvoiceInfo) {
    BusinessEntityInvoiceInfoModel businessEntityAddressModel = new BusinessEntityInvoiceInfoModel();
    ExtBeanUtils.copyProperties(supplierInvoiceInfo, businessEntityAddressModel);
    businessEntityAddressModel.setTerminalType("SUPPLIER");
    return (Long) this.hibernateTemplate.save(businessEntityAddressModel);
  }

  /**
   * 内部方法，所有列表查询都调用本方法 改方法是Hibernate 实现 注意增删改 业务必须调用该接口数据 不能用view后缀的数据
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  protected List<SupplierInvoiceInfo> superFindListPage(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "SUPPLIER"));
    return super.superFindListPage(wrapper, startRow, count);
  }

  /**
   * 内部方法，所有Sql执行的列表查询都调用本方法 改方法是 Mybatis 实现 注意增删改 业务不能调用该接口数据
   *
   * @param wrapper
   * @param startRow
   * @param count
   * @return
   */
  @Override
  protected <T> List<T> superFindListPageView(Wrapper wrapper, Integer startRow, Integer count) {
    wrapper.append(Restrictions.eq("terminalType", "SUPPLIER"));
    return super.superFindListPageView(wrapper, startRow, count);
  }

  @Override
  public int getCount(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "SUPPLIER"));
    return super.getCount(wrapper);
  }

  @Override
  public int getCountView(Wrapper wrapper) {
    wrapper.append(Restrictions.eq("terminalType", "SUPPLIER"));
    return super.getCountView(wrapper);
  }

  @Override
  protected SQL getViewSql() {
    return new SQL() {{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(BusinessEntityInvoiceInfoModel.class, "invo", null)
      );
      FROM(getModelTableName(BusinessEntityInvoiceInfoModel.class) + " invo");
    }};
  }

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    HashMap<String, String> weMap = new HashMap<>();
    weMap.put("companyName", "invo.COMPANY_NAME");
    weMap.put("taxpayerCode", "invo.TAXPAYER_CODE");
    weMap.put("businessEntityId", "invo.BUSINESS_ENTITY_ID");
    weMap.put("terminalType", "invo.TERMINAL_TYPE");
    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return weMap.get(propertyName);
    });
    return parse;
  }

  @Override
  protected Class getViewObject() {
    return InvoiceInfoVo.class;
  }

}
